package com.glowdayz.glowmee.bc;

import java.io.IOException;
import java.net.URLEncoder;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.MultipartBox;
import com.glowdayz.common.PageHandler;
import com.glowdayz.common.Util;
import com.glowdayz.common.MultipartBox.FileInfo;
import com.glowdayz.glowmee.cmd.AdminTableCMD;
import com.glowdayz.glowmee.dao.AdminTableDAO;
import com.glowdayz.glowmee.dao.BrandDAO;

public class ListEventBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
       
    public ListEventBC() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		String where = request.getParameter("where");
		if (where != null) {
			where = Util.getInstance().incoding(where);
		} else {
			where = "";
		}
		
		String boardTitle = request.getParameter("boardTitle");
		String brand = request.getParameter("brand");
		if (brand != null) {
			brand = Util.getInstance().incoding(brand);
		}
		String start_date = request.getParameter("start_date");
		String end_date = request.getParameter("end_date");
		if (boardTitle != null && !boardTitle.equals("")) {
			boardTitle = Util.getInstance().incoding(boardTitle);
			where += " AND a.boardTitle like '%" + boardTitle + "%'";
		}
		if (brand != null && !brand.equals("--- 선택하세요 ---")) {
			where += " AND a.idBrand = " + brand;
		}
		if (start_date != null && !start_date.equals("") && end_date != null && !end_date.equals("")) {
			start_date = start_date.replace("-", "");
			start_date += "000000";
			end_date += "999999";
			end_date = end_date.replace("-", "");
			where += " AND a.start_date BETWEEN " + start_date + " AND " + end_date;
		}
		
		
		String order = request.getParameter("order");
		request.setAttribute("sort", order);
		
		if (order != null && order.equals("isUnDisplay")) {
			order = "a.isDisplay ASC, a.create_date DESC";
			request.setAttribute("order", "&order=isUnDisplay");
		} else if (order != null && order.equals("isDisplay")) {
			order = "a.isDisplay DESC, a.create_date DESC";
			request.setAttribute("order", "&order=isDisplay");
		}
		
		if (order != null && order.equals("isUnReadCount")) {
			order = "a.readCount ASC, a.create_date DESC";
			request.setAttribute("order", "&order=isUnReadCount");
		} else if (order != null && order.equals("isReadCount")) {
			order = "a.readCount DESC, a.create_date DESC";
			request.setAttribute("order", "&order=isReadCount");
		}
		
		if (order != null && order.equals("isUnCommentCount")) {
			order = "count ASC, a.create_date DESC";
			request.setAttribute("order", "&order=isUnCommentCount");
		} else if (order != null && order.equals("isCommentCount")) {
			order = "count DESC, a.create_date DESC";
			request.setAttribute("order", "&order=isCommentCount");
		}
		
		String id_str = request.getParameter("id");
		String isDisplay_str = request.getParameter("isDisplay");
		if (id_str != null && isDisplay_str != null) {
			int id = Integer.parseInt(id_str);
			int isDisplay = Integer.parseInt(isDisplay_str);
			
			if (isDisplay == 1) {
				isDisplay = 0;
			} else if (isDisplay == 0) {
				isDisplay = 1;
			}
			
			AdminTableCMD cmd = new AdminTableCMD(id, isDisplay);
			try {
				AdminTableDAO.getInstance().isDisplayAdminTable(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('처리가 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		String page_str = request.getParameter("page");
		int page = 0; 
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		try {
			if (!where.equals("")) request.setAttribute("where", "&where=" + URLEncoder.encode(where, "UTF-8"));
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("brand", BrandDAO.getInstance().getBrandList(1, -1, "brandTitle", null));
			request.setAttribute("event", AdminTableDAO.getInstance().getEventList(page, 10, order, where));
			if (where.equals("")) {
				request.setAttribute("display", DBHandler.getInstance().getCount("admintable", "idAdminBoard", "boardType='EVENT' AND isDisplay='1'"));
				request.setAttribute("unDisplay", DBHandler.getInstance().getCount("admintable", "idAdminBoard", "boardType='EVENT' AND isDisplay='0'"));
			} else {
				request.setAttribute("display", DBHandler.getInstance().getCount(
						"admintable a", "idAdminBoard", "boardType='EVENT' AND isDisplay='1'" + where));
				request.setAttribute("unDisplay", DBHandler.getInstance().getCount(
						"admintable a", "idAdminBoard", "boardType='EVENT' AND isDisplay='0'" + where));
			}
			
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/listEvent.jsp");
		dpr.forward(request, response);
	}
	
	/** 이벤트를 추가할때 실행되는 메서드 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		
		MultipartBox mbox = MultipartBox.getInstance();
		FileInfo finfo = mbox.upload2(request);
		
		String boardTitle = mbox.getValue("boardTitle");
		int brand = mbox.getInt("brand");
		String start_date = mbox.getValue("start_date");
		start_date = start_date.replace("-", "").replace("T", "").replace(":", "") + "00";
		String end_date = mbox.getValue("end_date");
		end_date = end_date.replace("-", "").replace("T", "").replace(":", "") + "00";
		String boardText = mbox.getValue("boardText");
		int eventType = mbox.getInt("eventType");
		String eventLinkCode = mbox.getValue("eventLinkCode");
		
		try {
			AdminTableDAO.getInstance().insertEvent(new AdminTableCMD(
					eventType, eventLinkCode, boardTitle, boardText, brand, start_date, end_date, finfo));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		try {
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("event", AdminTableDAO.getInstance().getEventList(1, 10, null, null));
			request.setAttribute("display", DBHandler.getInstance().getCount("admintable", "idAdminBoard", "boardType='EVENT' AND isDisplay='1'"));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount("admintable", "idAdminBoard", "boardType='EVENT' AND isDisplay='0'"));
		} catch (Exception e) {
			DBHandler.getInstance().disconnect();
			mbox.deleteFile(finfo);
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/admin/listEvent.jsp");
		dpr.forward(request, response);
	}

}